package photobox.manage

import com.sun.image.codec.jpeg.JPEGCodec
import grails.converters.JSON
import photobox.BaseController
import photobox.account.PublicizeImg
import photobox.exception.NotFoundException
import photobox.utils.ImageUtils
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

import javax.imageio.ImageIO
import java.awt.image.BufferedImage

class MngPublicizeImgController extends BaseController{

    def publicizeImgService
    def index() {}
    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id","name", "photoPath", "linkurl", "isShow"], [sort: 'id', order: "desc"])

        def publicizeImgRs = publicizeImgService.list(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(publicizeImgRs.rs, publicizeImgRs.total, pageParams,
                [id: 'id', name: 'name', photoPath: 'photoPath', linkurl: 'linkurl',isShow:'isShow']) as JSON
    }
    def create(){
//        def _location = [
//                [id:PublicizeImg,text:'滚动图']
//        ]
//        [_location:_location]
    }
    def savePhoto() {
        String pictureUrl=uploadImage()//调用上传图片的方法，返回一个储//存图片的路径。
        //render([status:1,msg:"ok", img:pictureUrl] as JSON)
        render([status:1,msg:"ok", img:pictureUrl] as JSON)
    }
    def uploadImage(){//上傳圖片到本地文件夾中
        BufferedImage
        ImageIO
        JPEGCodec
        def filePathName
        def path
        def f = request.getFile ('file')//获取前台页面表单控//中的文件名称
        String pictureUrl =  request.getSession().getServletContext().getRealPath('images');
        if (!f.empty) {
            Date  d= new Date();
            String dateString=  d.format ("yyyy-mm-dd-hh-ss")
            String  fileProName=f.getOriginalFilename ()
            String extension = fileProName.split('\\.')[-1]  //截取获取文0. //件名的后缀
            filePathName = pictureUrl+"/" + dateString+"."+extension
            f.transferTo (new File(filePathName))
            path = "images/"+dateString+"."+extension
        }
        return  path   //返回文件名称
    }
    def save(){
        publicizeImgService.createPublicizeImg(params)
        render([error: 0, message: "创建新公布图片完成"] as JSON)
    }
    def edit(){
        def publicizeImg = PublicizeImg.get(params.id as Long)
        if (!publicizeImg) {
            throw new NotFoundException("没有找到对应的公布图片")
        }


        def imagePath =  publicizeImg.photoPath?'/photobox/'+publicizeImg.photoPath:'http://a.hiphotos.baidu.com/image/h%3D1050%3Bcrop%3D0%2C0%2C1680%2C1050/sign=00ad5dc433adcbef1e347a06999f15be/1c950a7b02087bf4dade33e9f0d3572c11dfcf84.jpg'
        return [imagePath:imagePath,publicizeImg: publicizeImg]
    }
    def update(){
        publicizeImgService.updatePublicizeImg(params)
        render([error: 0, message: "更新公布图片完成"] as JSON)
    }
    def delete(){
        publicizeImgService.deletePublicizeImg(params)
        render([error:0, message: "删除公布图片完成"] as JSON)
    }
}
